/**
 * 
 */
package com.jm.web.form.baseitem;


import javax.servlet.jsp.JspWriter;

import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringHelper;
import com.jm.web.form.support.FieldItem;
import com.jm.web.form.support.FormItem;
import common.Utils;

/**
 * @author yanjie
 *
 * 2011/10/6 上午9:33:27
 */
public class BaseItem extends FieldItem{
	protected Utils utils = null ;
	
	/**
	 * @param tag
	 */
	public BaseItem() {}
	
	@Override
	public void init() {
		super.init();
		prepareParams();
	}
	
	protected void prepareParams(){
		if( this.getBaseTag() != null && this.getBaseTag().getExtAttrList() != null)
		{
			for(String key : this.getBaseTag().getExtAttrList().keySet())
			{
				Object object = this.getBaseTag().getExtAttrList().get(key);
				if(object instanceof String){
					OnSetProperty(key, (String)object);
				}else if(object instanceof Integer){
					OnSetProperty(key, (String)object);
				}else if(object instanceof Double){
					OnSetProperty(key, (String)object);
				}else if(object instanceof Float){
					OnSetProperty(key, (String)object);
				}else{
					OnSetProperty(key, object);
				}
			}
		}
	}
	
	@Override
	public void OnSetProperty(String strName, Object object) {
		if("utils".equalsIgnoreCase(strName) && object instanceof Utils)
		{
			this.utils = (Utils)object;
			return;
		}
		super.OnSetProperty(strName, object);
	}
	
	public Utils getUtils() {
		if(this.utils == null && this.getBaseTag() != null && this.getBaseTag().getPage() != null){
			this.utils = this.getBaseTag().getPage().getUtils();
		}
		return utils;
	}
	
	/**
	 * @param writer
	 * for Tag
	 */
	public void onRender2(JspWriter writer) {
		renderItem(writer);
	}
	
	public void renderItem(JspWriter writer){
		renderBegin(writer);
		renderBody(writer);
		renderEnd(writer);
	}
	
	public void renderBegin(JspWriter writer){
		onRenderItemBegin(writer);
	}
	
	public void onRenderItemBegin(JspWriter writer) {
		try{
			writer.write(StringHelper.Format("<%1$s ",this.getNodeName()));
			AttributeBuilder attributesBuilder = new AttributeBuilder();
			attributesBuilder.InitFromHashtable(this.extAttrList,true);
			FillAttributeBuilder(attributesBuilder);
			writer.write(attributesBuilder.ToOutputString());
			writer.write(">");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void renderBody(JspWriter writer){
		if(this.getItems()  == null || this.getItems().size() == 0){
			currentIndex++;
			onRenderItemBody(writer,this,true);
		}else{
			for (int i = 0 ;i<this.getItems().size();i++) {
				Object object = this.getItems().get(i);
				if (object instanceof FormItem) {
					FormItem item = (FormItem) object;
					currentIndex++;
					
					onRenderItemBody(writer,item,false);
				}
			}
		}
	}
	
	public void onRenderItemBody(JspWriter writer,FormItem item,boolean bRenderNodeValue){
		super.outItemBodyHeader(writer,item);
		if(bRenderNodeValue)
			onRenderSingleBody(writer,item);
		else
			item.onRender(writer);
		outItemBodyFooter(writer,item);
	}
	
	public void onRenderSingleBody(JspWriter writer,FormItem item){
		try {
			writer.write(item.getNodeValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void renderEnd(JspWriter writer){
		onRenderItemEnd(writer);
	}
	
	public void onRenderItemEnd(JspWriter writer){
		try {
			writer.write(StringHelper.Format("</%1$s>",this.getNodeName()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
